<?php

namespace app\admin\controller;

use think\Controller;
use think\facade\Request;
use think\facade\Session;

class Address extends Controller
{
   
    public function index()
    {
        //
        $address=\app\common\model\Address::select();

        return $this->fetch('index',['address'=>$address]);
    }

    public function address_add()
    {
        return $this->fetch('address_add');
    }
    public function addressave(){
        $data=Request::param();
        $rule=[
            'user_id|地址编号'=>'require',
            'consignee|收货人'=>'require',
            'phone|手机'=>'require',
            'area|地区'=>'require',
            'address|地址'=>'require',
            

        ];
        $message=[
            'user_id'=>'编号不能为空',
            'consignee'=>'收货人不能为空',
            'phone'=>'手机不能为空',
            'area'=>'地区不能为空',
            'address'=>'地址不能为空',

        ];
        $validate=new \think\Validate($rule,$message);
        if($validate->check($data)){
            if(\app\common\model\Address::insert(['user_id'=>$data['user_id'],'consignee'=>$data['consignee'],'phone'=>$data['phone'],'area'=>$data['area'],'address'=>$data['address']]))
            {
                $this->success('添加成功','/admin/address/index');
            }else{
                $this->error('添加失败','/admin/address/index');
            }
        }else{
            $this->error($validate->getError(),'admin/index/index');
        }

    }
    public function delect($id){
        $id=Request::param();
        $id=$id['id'];
        if(\app\common\model\Address::where('id',$id)->delete()){
            $this->success('删除成功','admin/address/index');
        }else{
                $this->error('删除失败','admin/address/index');
        }
    }


    public function address_edit(){
        $id=Request::param();
        $id=$id['id'];
        $data=\app\common\model\Address::where('id',$id)->find();
        
       
        // dump($data);
        return $this->fetch('address_edit',['data'=>$data]);
    }
    public function addressed()
    {
        $data=Request::param();
        
        $rule=[
            'id|编号'=>'require',
            'user_id|地址编号'=>'require',
            'consignee|收货人'=>'require',
            'phone|手机'=>'require',
            'area|地区'=>'require',
            'address|地址'=>'require',
            

        ];
        $message=[
            'id'=>'编号不能为空',
            'user_id'=>'编号不能为空',
            'consignee'=>'收货人不能为空',
            'phone'=>'手机不能为空',
            'area'=>'地区不能为空',
            'address'=>'地址不能为空',

        ];
        $validate=new \think\Validate($rule,$message);
        if($validate->check($data)){
            $result=[
                'id'=>$data['id'],
                'user_id'=>$data['user_id'],
                'consignee'=>$data['consignee'],
                'phone'=>$data['phone'],
                'area'=>$data['area'],
                'address'=>$data['address'],
                
            ];
            if(\app\common\model\Address::where('id',$data['id'])->update($result)){
                $this->success('修改成功','/admin/address/index');
            }else{
                // dump($res);
                $this->error('修改失败','/admin/address/index'); 
            }
        }else{
            $this->error($validate->getError(),'admin/address/index');
        }
    }


}
